package com.c.yinyuezhushou.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.Service.MusicService;
import com.c.yinyuezhushou.Sql.MyDButil;

import de.hdodenhof.circleimageview.CircleImageView;

public class CoverFragment extends Fragment {
    private View rootView;
    private MusicItem musicItem;
    private CircleImageView circleImageView;
    private ImageView imageView;
    private MusicService musicService;
    private MyDButil myDButil;

    public CoverFragment(MusicItem musicItem){
        this.musicItem = musicItem;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_cover, container, false);
        circleImageView = rootView.findViewById(R.id.cover_cir);
        imageView = rootView.findViewById(R.id.cover_image);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MyApplication myApplication = (MyApplication) getActivity().getApplicationContext();

        myDButil = myApplication.getMyDButil();
        musicService = myApplication.getMusicService();
        setCir(musicItem);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myDButil.isMusicItemTable(musicItem,"LoveMusicItem")){
                    if (myDButil.removeMusicItemTable(musicItem,"LoveMusicItem")){
                        imageView.setImageResource(R.drawable.icon_love1);
                        Toast.makeText(getActivity(), "不爱了...", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    if (myDButil.setMusicItemTable(musicItem,"LoveMusicItem")){
                        imageView.setImageResource(R.drawable.icon_love1_1);
                        Toast.makeText(getActivity(), "爱了爱了", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public void setCir(MusicItem musicItem){
        this.musicItem = musicItem;
        Glide.with(getContext()).load(musicItem.getImageurl()).into(circleImageView);
        if (myDButil.isMusicItemTable(musicItem,"LoveMusicItem")){
            imageView.setImageResource(R.drawable.icon_love1_1);
        }else{
            imageView.setImageResource(R.drawable.icon_love1);
        }
    }
}
